//
//  HKUITool.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/10.
//

#import "HKUITool.h"
#import "HKCommonDefine.h"
#import "UIView+HKSafeArea.h"

@implementation HKUITool

+ (CGFloat)getIPhoneStatusBarHeight {
    CGFloat safeAreaTop = [HKKeyWindow ks_safeAreaInsets].top;
    return safeAreaTop > 0 ? safeAreaTop : 20.f;
}

+ (UIViewController *)getCurrentRootViewController {
    UIViewController *result;
    // Try to find the root view controller programmically
    // Find the top window (that is not an alert view or other window)
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows){
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    id nextResponder = [rootView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]){
        if ([nextResponder presentedViewController]) {
            result = [nextResponder presentedViewController];
        }else if ([nextResponder isKindOfClass:[UITabBarController class]]){
            result = [nextResponder selectedViewController];
        }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
            result = [(UINavigationController *)nextResponder visibleViewController];
        }else{
            result = nextResponder;
        }
    }else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil){
        result = topWindow.rootViewController;
    }else{
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    }
    return result;
}

+ (UINavigationController *)getCurrentNavgationController{
    UIViewController *topViewController = [self getCurrentRootViewController];
    if ([topViewController isKindOfClass:UITabBarController.class]) {
        UITabBarController *tabbarVC = (UITabBarController *)topViewController;
        return tabbarVC.selectedViewController;
    } else {
        UINavigationController *navagationVC = (UINavigationController *)topViewController;
        return navagationVC;
    }
}

+ (void)setStatusBarBackgroundColor:(UIColor *)color {
    if (@available(iOS 13.0, *)) {
           UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
            statusBar.backgroundColor = color;
            [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
        } else {
            // Fallback on earlier versions
            UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
               if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
                   statusBar.backgroundColor = color;
               }
        }
}

@end
